package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;

import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService tagService;
    @Reference
    private IStrategyCommentService commentService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategyCommendService commendService;
    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;


    //进入攻略主页
    @RequestMapping("")
    public String index(Model model){
        //commends
        List<StrategyCommend> list = commendService.getTop5();
        model.addAttribute("commends",list);
        //hotCds
        List<StrategyStatisVO> hots = strategyStatisRedisService.listHots();
        model.addAttribute("hotCds",hots);

        //所有推荐攻略
        List<StrategyStatisVO> commends = strategyStatisRedisService.listCommends();
        //国外
        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        //国内
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();
        for (StrategyStatisVO commend : commends) {
            if (commend.isIsabroad()){
                //1 ---> 国外
                if (abroadCds.size() < 10){
                    abroadCds.add(commend);
                }
            }else {
                //0 ---> 国内
                if (unabroadCds.size() < 10){
                    unabroadCds.add(commend);
                }
            }
        }
        model.addAttribute("abroadCds",abroadCds);
        model.addAttribute("unabroadCds",unabroadCds);

        //themeCds
        List<Map<String, Object>> themeCommend = strategyTemplateSearchService.getThemeCommend();
        model.addAttribute("themeCds",themeCommend);

        //主题攻略回显 themes
        List<StatisVO> themes = strategyTemplateSearchService.queryConditionThemeGroup();
        model.addAttribute("themes",themes);

        //chinas
        List<StatisVO> chinas = strategyTemplateSearchService.queryConditionProvincesGroup();
        model.addAttribute("chinas",chinas);

        //abroads
        List<StatisVO> abroads = strategyTemplateSearchService.queryConditionAbroadsGroup();
        model.addAttribute("abroads",abroads);

        return "/strategy/index";
    }

    @RequestMapping("/detail")
    public String detail(Long id, Model model){
        StrategyDetail detail = detailService.get(id);
        detail.setStrategyContent(detailService.getContentByDetailId(id));
        model.addAttribute("detail",detail);

        //每次访问攻略明细,阅读数 + 1
        strategyStatisRedisService.addViewnum(id,1);
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVO(id);
        model.addAttribute("vo",vo);

        //热门(浏览)攻略分数+1
        strategyStatisRedisService.addHotScore(id,1);

        return "/strategy/detail";
    }

    //查询某个目的地的全部攻略
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //dest
        Destination dest = toasts.remove(toasts.size() - 1);  //城市
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        //tags
        List<StrategyTag> tags = tagService.list();
        model.addAttribute("tags",tags);

        //pageInfo
        PageInfo pageInfo = detailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/strategy/list";
    }

    //添加评论
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(@UserParam UserInfo userInfo, StrategyComment strategyComment){
        if (userInfo == null){
            throw new LogicException("大哥,登录先");
        }
        strategyComment.setUserId(userInfo.getId());
        strategyComment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyComment.setLevel(userInfo.getLevel());
        strategyComment.setCity(userInfo.getCity());
        strategyComment.setUsername(userInfo.getNickname());
        commentService.save(strategyComment);

        //每增加一条评论,评论数 + 1
        strategyStatisRedisService.addReplynum(strategyComment.getDetailId(),1);
        int replynum = strategyStatisRedisService.getStrategyStatisVO(strategyComment.getDetailId()).getReplynum();

        //热门(评论)攻略分数+1
        strategyStatisRedisService.addHotScore(strategyComment.getDetailId(),1);

        return AjaxResult.SUCCESS.addData(replynum);
    }

    //显示评论
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo")StrategyCommentQuery qo){
        Page page = commentService.query(qo);
        model.addAttribute("page",page);
        return "/strategy/commentTpl";
    }

    //点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, Long fromid){
        commentService.commentThumbUp(toid,fromid);
        return AjaxResult.SUCCESS;
    }

    //收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(@UserParam UserInfo userInfo, Long sid){
        if(userInfo == null){
            AjaxResult ajaxResult = new AjaxResult(false, "登录先");
            ajaxResult.setCode(102);
            return ajaxResult;
        }
        //true则收藏,否则取消收藏
        boolean result = strategyStatisRedisService.addFavornum(sid, userInfo.getId());

        if (result){
            //热门(收藏)推荐分数+1
            strategyStatisRedisService.addCommendScore(sid,1);
        }else{
            //热门(收藏)推荐分数-1
            strategyStatisRedisService.addCommendScore(sid,-1);
        }

        return new AjaxResult(result,"").addData(strategyStatisRedisService.getStrategyStatisVO(sid));
    }

    //顶(点赞)
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(@UserParam UserInfo userInfo,Long sid){
        if(userInfo == null){
            AjaxResult ajaxResult = new AjaxResult(false, "登录先");
            ajaxResult.setCode(102);
            return ajaxResult;
        }
        boolean result = strategyStatisRedisService.addThumbsupnum(sid, userInfo.getId());
        //热门(点赞)推荐分数+1
        if (result){
            strategyStatisRedisService.addCommendScore(sid,1);
        }

        return new AjaxResult(result,"").addData(strategyStatisRedisService.getStrategyStatisVO(sid));
    }

    //攻略地下(最受欢迎|更新时间)
    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo") SearchQueryObject qo){
        Page page = strategyTemplateSearchService.query(qo);
        model.addAttribute("page",page);
        return "/strategy/searchPageTpl";
    }


    //分享
    @RequestMapping("/strategyShare")
    @ResponseBody
    public Object strategyShare(Long sid){
        //直接分享+1 无论登录或者没有登录都可以分享
        strategyStatisRedisService.addSharenum(sid,1);
        //通过攻略id查询vo对象返回给前端
        return new AjaxResult().addData(strategyStatisRedisService.getStrategyStatisVO(sid));
    }

    //分享后的链接用restful风格进入
    @RequestMapping("/detail/{sid}")
    public String shareAfter(Model model,@PathVariable Long sid){
        StrategyDetail detail = detailService.get(sid);
        detail.setStrategyContent(detailService.getContentByDetailId(sid));
        model.addAttribute("detail",detail);

        //每次访问攻略明细,阅读数 + 1
        strategyStatisRedisService.addViewnum(sid,1);
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVO(sid);
        model.addAttribute("vo",vo);

        //热门(浏览)攻略分数+1
        strategyStatisRedisService.addHotScore(sid,1);
        return "/strategy/detail";
    }

}
